﻿namespace com.oakwoodsys.SharePoint
{
    using System;

    using com.oakwoodsys.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;

    public class SharePointUri : Uri
    {
        #region Constructors

        public SharePointUri( string uriString )
            : base(uriString)
        {
            UriString = uriString.TrimEnd( '/' );
            RootFolderName = ListUrl = Title = SiteUrl = WebUrl = string.Empty;
            initialize( );
        }

        #endregion Constructors

        #region Properties

        public Guid? ID
        {
            get;
            private set;
        }

        public bool ListFound
        {
            get;
            private set;
        }

        public string ListUrl
        {
            get;
            private set;
        }

        public string RootFolderName
        {
            get;
            private set;
        }

        public string SiteUrl
        {
            get;
            private set;
        }

        public string Title
        {
            get;
            private set;
        }

        public string WebUrl
        {
            get;
            private set;
        }

        string UriString
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        void initialize( )
        {
            string s = UriString;
            using ( SPSite spSite = new SPSite( UriString ) )
            {
                SiteUrl = spSite.Url;
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    WebUrl = spWeb.Url;
                    if ( s.EndsWith( ".aspx" ) )
                    {
                        int i = s.LastIndexOf( '/' );
                        s = s.Substring( 0, i );
                    }
                    bool ok = false;
                    do
                    {
                        int i = s.LastIndexOf( '/' );
                        string r = s.Substring( i + 1 );
                        if ( i < 0 )
                            break;
                        s = s.Substring( 0, i );
                        SPList spList = spWeb.Lists.GetListByRootFolder( r );
                        if ( spList != null )
                        {
                            string rootFolderName = spList.RootFolder.Name;
                            string parentFolder = spList.RootFolder.Url;
                            string listUrl = string.Concat( WebUrl, '/', parentFolder );
                            if ( UriString.StartsWith( listUrl, StringComparison.OrdinalIgnoreCase ) )
                            {
                                ListUrl = listUrl;
                                RootFolderName = rootFolderName;
                                Title = spList.Title;
                                ID = spList.ID;
                                ok = true;
                                break;
                            }
                        }
                    } while ( !ok );
                    ListFound = ok;
                }
            }
        }

        #endregion Methods
    }
}